<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_package_order extends _init_model
	{
		public $PAK_O_ID;
		public $PAK_O_CODE;
		public $PAK_O_DATE;
		public $PAK_O_TIME;
		public $PAK_O_DAY_AMOUNT;
		public $MEM_ID;
		public $CAF_ID;
		public $PAK_ID;
		public $PAK_O_STATUS;
		
		function __construct(){
			parent::__construct();
		
			$this->tableName = "package_order";

			$this->load->library('custom_datetime');
			$this->PAK_O_CODE = $this->gen_order_code();
			$this->PAK_O_DATE = $this->custom_datetime->current_date();
			$this->PAK_O_TIME = $this->custom_datetime->current_time();
			$this->PAK_O_STATUS = 0;
		}
			
		function gen_order_code(){
			$this->db->select_max('PAK_O_ID');
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$row = $query->row_array();								
				return 'PK' . date('y') . date('m') . str_pad($row['PAK_O_ID']+1, 5, '0', STR_PAD_LEFT);				
			} else
				return 'PK' . date('y') . date('m') . '00000';
		}
		function get_single_order($order_code){
			$this->db->select(array('package_order.PAK_O_CODE', 'package_order.PAK_O_DATE', 'package_order.PAK_O_TIME', 'package_order.PAK_O_DAY_AMOUNT', 'package_order.PAK_O_STATUS',					
									'package.PAK_NAME', 'package.PAK_ID', 'package.PAK_PRICE',
									'cafe.CAF_TITLE', 'cafe.CAF_NAME', 'cafe.CAF_ID',
									'member.MEM_SNAME', 'member.MEM_FNAME', 'member.MEM_USERNAME'));
			$this->db->join('package', 'package.PAK_ID = package_order.PAK_ID');
			$this->db->join('cafe', 'cafe.CAF_ID = package_order.CAF_ID');
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'LEFT');
			$this->db->join('member', 'member.MEM_ID = package_order.MEM_ID');
			$this->db->where('PAK_O_CODE', $order_code);
			$query = $this->db->get($this->tableName);
					
			if ($query->num_rows() > 0){
				$row = $query->row_array(0);	

				$this->load->library('define/directory_path');
				if (!empty($row['CAF_IMG_L_FILENAME']))
					$row['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $row['CAF_IMG_L_FILENAME'];
				else
					$row['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
								
				return $row;
			} else
				return null;
		}
		function get_payment_order_list($display_length=null, $start_index=null){
			$this->db->join('package', 'package.PAK_ID = package_order.PAK_ID');
			$this->db->join('cafe', 'cafe.CAF_ID = package_order.CAF_ID', 'left');
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'LEFT');		
			$this->db->order_by('package_order.PAK_O_ID', 'DESC');					
			
			if (!is_null($start_index) && !is_null($display_length))
				$query = $this->db->get($this->tableName, $display_length, $start_index);
			else
				$query = $this->db->get($this->tableName);
			
			$return = $query->result_array();
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				
				for ($i=0; $i<count($return); $i++){
					if (isset($return[$i]['CAF_IMG_L_FILENAME'])){
						if (!empty($return[$i]['CAF_IMG_L_FILENAME']))
							$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
						else
							$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					}
				}
			}
			
			return $return;	
		}
		
		function get_new_order_list($display_length=null, $start_index=null){
			$this->db->where('PAK_O_STATUS', 0);
			return $this->get_payment_order_list($display_length, $start_index);
		}
		function get_payment_history($member_id, $display_length=null, $start_index=null){
			$this->db->select(array('package_order.PAK_O_CODE', 'package_order.PAK_O_DATE', 'package_order.PAK_O_TIME', 'package_order.PAK_O_STATUS',
									'cafe.CAF_TITLE', 'cafe_img_logo.CAF_IMG_L_FILENAME', 'package.PAK_NAME'));
			$this->db->where('package_order.MEM_ID', $member_id);
			$this->db->where('PAK_O_STATUS !=', 0);
			return $this->get_payment_order_list($display_length, $start_index);
		}
				
		function get_new_order_count(){
			$this->db->where('PAK_O_STATUS', 0);
			return $this->db->count_all_results($this->tableName);
		}
		function get_payment_order_count($member_id=null){
			if (!is_null($member_id))
				$this->db->where('MEM_ID', $member_id);
				
			$this->db->where('PAK_O_STATUS !=', 0);
			return $this->db->count_all_results($this->tableName);
		}	
		
		function set_deny_order_by_ordercode($order_code){
			$this->db->where('PAK_O_CODE', $order_code);
			return $this->db->update($this->tableName, array('PAK_O_STATUS' => 2));
		}
		function set_accept_order_by_ordercode($order_code){
			$this->db->where('PAK_O_CODE', $order_code);
			return $this->db->update($this->tableName, array('PAK_O_STATUS' => 1));
		}
		
		function add(){
			return $this->db->insert($this->tableName, array('PAK_O_CODE' => $this->PAK_O_CODE,
																'PAK_O_DATE' => $this->PAK_O_DATE,
																'PAK_O_TIME' => $this->PAK_O_TIME,
																'PAK_O_DAY_AMOUNT' => $this->PAK_O_DAY_AMOUNT,
																'MEM_ID' => $this->MEM_ID,
																'CAF_ID' => $this->CAF_ID,
																'PAK_ID' => $this->PAK_ID,
																'PAK_O_STATUS' => $this->PAK_O_STATUS));
		}
	}
?>